<?php

namespace catchAdmin\model;

use catcher\Tree;
use think\facade\Cache;

class SysVocation extends ModelApp
{


    protected $append = [
        'status_name',
        'sys_is_name',
    ];

    public $str   = "电气,熔断器,超高压,电源,电容器,互感器,分压器,稳压电路,避雷器,节电设备,继电器,电子电器,插头,插座,电钻,变压器,传感器,变频,高压变频,互感器,电抗器,调压器,变频器,电石,电解,电力变压器,开关柜,配电箱,电厂设备,开关设备,配电柜,控制柜,断路器,配电设备,配电,电厂";
    public $str_2 = "电力设备,电表箱,电网,变压器,封闭母线,开关柜,隔离开关,抗电器,并联电容,电线,电缆,直流系统,发电机,电能表,汽轮机,辅机设备,锅炉,配电设备,配电,稳压器,电源,电力设备,光缆,电动机,电力材料,电力维修,设备维护,保险丝,电气设施,接插件,电力电子设备";
    public $str_3 = "高压控制,电厂控制,电站控制,直流系统,水电站,电热厂,核电,风电场,风力发电,太阳能发电,光伏发电,生物质发电,电力工程,配电工程,变电工程,电输送,电流互感,电力改造,电力项目,电力公司,火电厂,风力电站,太阳能电站,核电站,送变电,变电站,配电工程,电力施工,架线工程,用电工程,路灯工程,线路工程,电缆工程,照明改造,弱电智能化工程,供电工程,电气工程,电站工程,火力发电,风电机组,风电厂,核电厂,发电站,动力厂,动力车间,电厂安装,自备电厂,变配电室,焦化电厂,减压站,配电站,调压站";


    ################################### 关系 start ###########################

    public function parent()
    {
        return $this
            ->hasOne(SysVocation::class, 'id', 'parent_id')
            ->bind([
                'parent_vocation_name' => 'vocation_name',
            ]);
    }

    ################################### 关系 end ###########################
    ################################### Attr start ###########################

    /**
     * status_name_list
     */
    public function getStatusNameList()
    {
        return [
            1 => '审核中',
            2 => '审核通过',
            3 => '审核未通过',
        ];
    }


    /**
     * status_name
     */
    public function getStatusNameAttr()
    {
        return $this->getStatusNameList()[$this->getOrigin('status')] ?? '';
    }


    /**
     * sys_is_name_list
     */
    public function getSysIsNameList()
    {
        return [
            1 => '系统',
            2 => '用户自定义',
        ];
    }


    /**
     * sys_is_name
     */
    public function getSysIsNameAttr()
    {
        return $this->getSysIsNameList()[$this->getOrigin('sys_is')] ?? '';
    }

    ################################### Attr end ###########################
    ############################ search start ###############################

    public function searchVocationNameAttr($query, $value, $data)
    {
        $query->where('vocation_name', 'like', '%' . $value . '%');
    }


    public function searchParentVocationNameAttr($query, $value, $data)
    {
        $query->where('parent_id', 'IN', function ($query) use ($value) {
            $query
                ->table('sys_vocation')
                ->where('vocation_name', 'like', '%' . $value . '%')
                ->field('id');
        });
    }

    ############################ search end ###############################
    ################################### func start ###########################

    public function indexTree()
    {
        if (!empty(Cache::get('sys_vocation_tree'))) {
            return Cache::get('sys_vocation_tree');
        }

        $list = Tree::done($this
            ->select()
            ->toArray());

        $list_2 = [];
        foreach ($list as $item) {
            $tmp = [
                'value' => $item['id'],
                'label' => $item['vocation_name'],
            ];
            if (!empty($item['children'])) {
                foreach ($item['children'] as $child) {
                    $tmp_2 = [
                        'value' => $child['id'],
                        'label' => $child['vocation_name'],
                    ];
                    if (!empty($child['children'])) {
                        foreach ($child['children'] as $child_2) {
                            $tmp_2['children'][] = [
                                'value' => $child_2['id'],
                                'label' => $child_2['vocation_name'],
                            ];
                        }
                        $tmp['children'][] = $tmp_2;
                    } else {
                        $tmp['children'][] = $tmp_2;
                    }
                }
            }

            $list_2[] = $tmp;
        }

        Cache::set('sys_vocation_tree', $list_2);

        return $list_2;
    }

    ################################### func end ###########################

}
